package com.clean.cleantotal.compores.result

import android.graphics.Color
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.SPUtils
import com.clean.cleantotal.R
import com.clean.cleantotal.base.BaseActivity
import com.clean.cleantotal.bean.DataUtils
import com.clean.cleantotal.bean.FunType
import com.clean.cleantotal.bean.HomeBean
import com.clean.cleantotal.compores.app.ScanAppActivity
import com.clean.cleantotal.compores.battery.BatteryActivity
import com.clean.cleantotal.compores.clean.NewCleanScanActivity
import com.clean.cleantotal.compores.compress.CompressActivity
import com.clean.cleantotal.compores.device.DeviceActivity
import com.clean.cleantotal.compores.empty.EmptyActivity
import com.clean.cleantotal.compores.large.LargeActivity
import com.clean.cleantotal.compores.process.ProcessActivity
import com.clean.cleantotal.databinding.ActivityResultBinding
import com.clean.cleantotal.databinding.HeadResultLayoutBinding
import com.clean.cleantotal.utils.FormatterSize
import com.clean.cleantotal.utils.Utils
import com.drake.brv.utils.linear
import com.drake.brv.utils.setup

class ResultActivity : BaseActivity<ActivityResultBinding>() {
    override val viewBinding: ActivityResultBinding
        get() = ActivityResultBinding.inflate(layoutInflater)

    private var list = ArrayList<Any>()
    private var funType: FunType? = null
    private var totalSize: Long = 0L
    private var title = ""

    private val height = 640 // 滑动开始变色的高,真实项目中此高度是由广告轮播或其他首页view高度决定
    private var overallXScroll = 0

    override fun initParams(extras: Bundle?) {
        super.initParams(extras)
        extras?.let {
            funType = it.getSerializable("funType") as FunType?
            totalSize = it.getLong("totalSize", 0L)
            title = it.getString("title", "")
        }
    }

    override fun initView() {
        try {


            mBinding.recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    // 累加y值 解决滑动一半y值为0
                    overallXScroll += dy
                    //设置标题的背景颜色
                    if (overallXScroll <= 0) {
                        mBinding.titleLayout.setBackgroundColor(Color.argb(0, 255, 255, 255))
                    } else if (overallXScroll in 1..height) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
                        val scale = overallXScroll.toFloat() / height
                        val alpha = 255 * scale
                        mBinding.titleLayout.setBackgroundColor(
                            Color.argb(
                                alpha.toInt(), 6, 197, 137
                            )
                        )
                    } else {
                        mBinding.titleLayout.setBackgroundColor(Color.argb(255, 6, 197, 137))
                    }
                }
            })


            if (funType == FunType.COMPRESS) {
                ActivityUtils.finishActivity(CompressActivity::class.java)
            }
            val cleanTotalSize = SPUtils.getInstance().getLong("cleanTotalSize", 0L)
            SPUtils.getInstance().put("cleanTotalSize", cleanTotalSize + totalSize)
            mBinding.back.setOnClickListener { onBackPressed() }
            list.add(true)
            val resultList = Utils.updateIndex(DataUtils.getResultList())
            list.addAll(resultList)
            val headHomeBean = DataUtils.getTjList(funType)
            mBinding.recyclerView.linear().setup {
                addType<Boolean> { R.layout.head_result_layout }
                addType<HomeBean> { R.layout.item_result_layout }
                onBind {
                    when (itemViewType) {
                        R.layout.head_result_layout -> {
                            val head = HeadResultLayoutBinding.bind(itemView)
                            head.tjLogo.load(headHomeBean.icon)
                            head.funTitle.text = headHomeBean.title
                            head.funContent.text = headHomeBean.content
                            head.funContent.isVisible = headHomeBean.content.isNotEmpty()
                            when (funType) {
                                FunType.LARGE, FunType.CLEAN -> {
                                    head.tvTitle.text = "Clean up"
                                    head.tvContent.text =
                                        "Congratulations on cleaning \n everything up"
                                    head.yellowYuan.isVisible = true
                                    val size = FormatterSize.formatFileSizeSplit(
                                        this@ResultActivity, totalSize
                                    )
                                    head.tvSize.text = size.first()
                                    head.tvUnit.text = size.last()
                                    head.ivFinish.isVisible = false
                                    head.fondYuan.isVisible = false
                                }

                                else -> {
                                    head.yellowYuan.isVisible = false
                                    head.tvTitle.text = "Finished !"
                                    head.tvContent.text = ""
                                    head.ivFinish.isVisible = true
                                    head.fondYuan.isVisible = true
                                }
                            }

                        }
                    }
                }
                onClick(R.id.btnClean) {
                    clickItem(headHomeBean)
                }
                onClick(R.id.resultLayout) {
                    val model = getModel<HomeBean>()
                    clickItem(model)
                }
            }.models = list

            if (title.isNotEmpty()) {
                mBinding.title.text = title
            } else {
                val sList = resultList.filter { it.funType == funType }
                if (sList.isNotEmpty()) {
                    val first = sList.first()
                    first?.let {
                        mBinding.title.text = it.title
                    } ?: run {
                        mBinding.title.text = "Clean"
                    }
                } else {
                    mBinding.title.text = "Clean"
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun clickItem(model: HomeBean) {
        when (model.funType) {
            FunType.DEVICE -> {
                openActivity(DeviceActivity::class.java)
                finish()
            }

            FunType.LARGE -> {
                Utils.requestPr(this) {
                    openActivity(LargeActivity::class.java)
                    finish()
                }
            }

            FunType.BATTERY -> {
                openActivity(BatteryActivity::class.java)
                finish()
            }

            FunType.PROCESS -> {
                openActivity(ProcessActivity::class.java)
                finish()
            }

            FunType.EMPTY -> {
                Utils.requestPr(this) {
                    openActivity(EmptyActivity::class.java)
                    finish()
                }
            }

            FunType.COMPRESS -> {
                Utils.requestPr(this) {
                    openActivity(CompressActivity::class.java)
                    finish()
                }
            }

            FunType.TIK, FunType.INS, FunType.TEL, FunType.WHATSAPP -> {
                Utils.requestPr(this) {
                    openActivity(ScanAppActivity::class.java, Bundle().apply {
                        putSerializable("funType", model.funType)
                        putString("title", model.title)
                    })
                    finish()
                }
            }

            FunType.CLEAN -> {
                Utils.requestPr(this) {
                    openActivity(NewCleanScanActivity::class.java)
                    finish()
                }
            }

            else -> {

            }
        }
    }

}